package com.housekeeping.housekeeping.advertisingmanagement.controller.productOrder;/*
 * @author  pyh
 * @date  2021/1/23 0023 下午 5:49
 * @version 1.0
 * @since JDK1.8_241
 */

import com.housekeeping.housekeeping.advertisingmanagement.entity.ProductOrder;
import com.housekeeping.housekeeping.advertisingmanagement.service.ProductOrderService;

import com.housekeeping.housekeeping.advertisingmanagement.service.impl.ProductOrderServiceImpl;
import com.housekeeping.housekeeping.advertisingmanagement.utils.PageUtil;
import com.housekeeping.housekeeping.advertisingmanagement.utils.ResponseEntitys;
import com.housekeeping.housekeeping.order.utils.ResponseCode;
import com.housekeeping.housekeeping.order.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//通过订单号(number)查询订单信息
public class POSSearchById extends HttpServlet {
    private ProductOrderService spos = new ProductOrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String number = req.getParameter("number");
        PageUtil<List<ProductOrder>> orders = new PageUtil<>();
        ArrayList<ProductOrder> list = new ArrayList<>();
        ResponseEntitys<PageUtil<List<ProductOrder>>> entity = null;
        //调用业务逻辑
        try {
            ProductOrder orderContent = spos.getOrderContent(number);
            if (orderContent != null) {
                list.add(orderContent);         //为了配合前端的json分页解析，用数组包裹
                orders.setData(list);
                orders.setTotalCount(1);         //设置为1就好，因为根据订单号查询，结果是唯一的

                entity = ResponseEntitys.success(orders);
            } else {
                //报订单查询有误
                entity = ResponseEntitys.error(ResponseCode.ORDER_SEARCH_ERROR);
            }
        } catch (SQLException throwables) {
            entity = ResponseEntitys.error(ResponseCode.ORDER_SEARCH_ERROR);
        }

        ResponseUtils.deliver(resp, entity);
    }
}
